package com.lee.jedis;

import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lihaiqiang
 * @date 2022/1/19
 */
public class JedisTest {

    public static void main(String[] args) throws Exception {
//        testBaseJedis();
        //testPoolJedis();
        testJedisCluster();
    }

    /**
     * 基本测试
     */
    private static void testBaseJedis() {
        // 创建 redis 对象，指定 host 和 port
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        // 账号、密码
        //jedis.auth("password");
        //jedis.auth("user", "password");

        test(jedis);

        jedis.close();
    }

    /**
     * 线程池测试
     */
    private static void testPoolJedis() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(8);
        config.setMaxTotal(18);
        //
        JedisPool pool = new JedisPool(config, "127.0.0.1", 6379, 2000);
        Jedis jedis = pool.getResource();

        test(jedis);

        jedis.close();
        pool.close();
    }

    /**
     * 集群测试
     */
    private static void testJedisCluster() {
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("127.0.0.1", 6379));
        nodes.add(new HostAndPort("127.0.0.1", 6380));
        nodes.add(new HostAndPort("127.0.0.1", 6381));
        nodes.add(new HostAndPort("127.0.0.1", 6382));
        nodes.add(new HostAndPort("127.0.0.1", 6383));
        nodes.add(new HostAndPort("127.0.0.1", 6384));

        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMinIdle(1);
        poolConfig.setMaxIdle(5);
        poolConfig.setMaxTotal(10);
        JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig);

        testCluster(jedisCluster);

        jedisCluster.close();
    }

    private static void test(Jedis jedis) {
        long startTime = System.currentTimeMillis();
        // 新增
//        StringBuilder stringBuilder = new StringBuilder();
//        for (int i = 0; i < 5000000; i++) {
//            String mac = String.format("%012x", i);
//            stringBuilder.append(mac);
//            for (int j = mac.length() - 2; j > 0; j -= 2) {
//                stringBuilder.insert(j, ":");
//            }
//            //System.out.println(stringBuilder);
//            jedis.sadd("DD", stringBuilder.toString());
//            stringBuilder.delete(0, stringBuilder.length());
//        }

//        System.out.println(String.format("%tT", new Date()) + " " + jedis.scard("DD"));
//        Set<String> set = jedis.smembers("DD");
//        System.out.println("size: " + set.size());
//        System.out.println(set.iterator().next());

        // 获取
        ScanParams scanParams = new ScanParams();
        scanParams.count(1000000);
        String cursor = "0";
        List<String> result;
        do {
            ScanResult<String> scanResult = jedis.sscan("DD", cursor, scanParams);
            result = scanResult.getResult();
            cursor = scanResult.getCursor();
            System.out.println("result size: " + result.size());
            System.out.println("cursor: " + cursor);
        } while (!"0".equals(cursor));

        System.out.println("耗时：" + (System.currentTimeMillis() - startTime));
    }

    private static void testCluster(JedisCluster jedisCluster) {
        long startTime = System.currentTimeMillis();

        // 新增
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 30000; i++) {
            String mac = String.format("%012x", i);
            stringBuilder.append(mac);
            for (int j = mac.length() - 2; j > 0; j -= 2) {
                stringBuilder.insert(j, ":");
            }
            //System.out.println(stringBuilder);
            jedisCluster.sadd("DD", stringBuilder.toString());
            stringBuilder.delete(0, stringBuilder.length());
        }

//        // 获取1
//        Set<String> set = jedisCluster.smembers("DD");
//        System.out.println("size: " + set.size());

//        // 获取2
//        ScanParams scanParams = new ScanParams();
//        scanParams.count(1000000);
//        String cursor = "0";
//        List<String> macList = new ArrayList<>();
//        List<byte[]> result;
//        do {
//            long t = System.currentTimeMillis();
//            ScanResult<byte[]> scanResult = jedisCluster.sscan("DD".getBytes(StandardCharsets.UTF_8),
//                    cursor.getBytes(StandardCharsets.UTF_8), scanParams);
//            result = scanResult.getResult();
//            cursor = scanResult.getCursor();
//
//
//            for (byte[] bytes : result) {
//                macList.add(new String(bytes, StandardCharsets.UTF_8));
//            }
//
//            System.out.println("result size: " + result.size());
//            System.out.println("cursor: " + cursor);
//            System.out.println("耗时: " + (System.currentTimeMillis() - t));
//        } while (!"0".equals(cursor));
//
//        System.out.println("===========================================");
//        System.out.println("total size: " + macList.size());

//        // 移除
//        System.out.println(jedisCluster.del("DD"));

        System.out.println("总耗时：" + (System.currentTimeMillis() - startTime));
    }
}
